package com.taomu.app.zongchouhuli.moudle.mine.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.entity.BaseFinishEntity;
import com.taomu.app.zongchouhuli.framework.BaseFragment;
import com.taomu.app.zongchouhuli.http.res.ResOrderEntity;
import com.taomu.app.zongchouhuli.http.res.ResOrderListEntity;
import com.taomu.app.zongchouhuli.moudle.home.activity.HomeOrderSubmitActivity;
import com.taomu.app.zongchouhuli.moudle.home.activity.HomePayActivity;
import com.taomu.app.zongchouhuli.moudle.mine.activity.UserCashActivity;
import com.taomu.app.zongchouhuli.moudle.mine.activity.UserConfirmRechargeActivity;
import com.taomu.app.zongchouhuli.moudle.mine.activity.UserRechargeActivity;
import com.taomu.app.zongchouhuli.moudle.mine.adpter.OrderListAdp;
import com.taomu.app.zongchouhuli.moudle.mine.contract.OrderListContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.OrderListPresenter;
import com.taomu.app.zongchouhuli.moudle.mine.entity.OrderEntity;
import com.taomu.app.zongchouhuli.utils.ListViewUtils;
import com.taomu.app.zongchouhuli.utils.LoadingDialog;
import com.taomu.app.zongchouhuli.utils.RxBus2;
import com.taomu.app.zongchouhuli.utils.TokenDialog;
import com.taomu.app.zongchouhuli.weight.EmptyRecyclerView;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class OrderListFragment extends BaseFragment<OrderListContract.Presenter> implements OrderListContract.View {

    public static final String ORDERTYPEID = "ordertypeid";

    private EmptyRecyclerView mRecy;

    private OrderListAdp mAdp;

    private SmartRefreshLayout mRefreshLayout;

    @Override
    public void onDestroy() {
        mPresenter.onDetch();
        super.onDestroy();
    }

    public static OrderListFragment getInstence(String typeid){
        OrderListFragment fragment = new OrderListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ORDERTYPEID,typeid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order_list;
    }

    @Override
    protected void initView(View view) {
        mDialog = LoadingDialog.createLoadingDialog(getContext(), "加载中...");
        mRefreshLayout = view.findViewById(R.id.order_list_refresh);
        mAdp = new OrderListAdp(getContext());
        mRecy = view.findViewById(R.id.order_list_recy);
        mRecy.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecy.setAdapter(mAdp);
        ListViewUtils.setDefaultEmpty(mRecy);
    }

    private String typeid = "-1";

    @Override
    protected void initData() {
        if(getArguments() != null){
            typeid = getArguments().getString(ORDERTYPEID, "-1");
        }
    }

    @Override
    protected void initEvent() {
        mAdp.setOrderListListener(new OrderListAdp.OrderListListener() {
            @Override
            public void onRechagerClick(ResOrderListEntity.DataBean entity) {
                if("order_recharge".equals(entity.getOrder_type_id())){
                    Intent intent = new Intent(getContext(),UserRechargeActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                }else if("order_forward".equals(entity.getOrder_type_id())){
                    Intent intent = new Intent(getContext(),UserCashActivity.class);
                    startActivity(intent);
                }
            }

            @Override
            public void onConfrimClick(ResOrderListEntity.DataBean entity) {
                UserConfirmRechargeActivity.start(getContext(), String.valueOf(entity.getProduct_id()), entity.getOrder_id(), entity.getTotal_price(), entity.getOrder_id());
            }

            @Override
            public void onDelteClick(ResOrderListEntity.DataBean entity) {
                mPresenter.deleteOrder(entity.getOrder_id());
            }

            @Override
            public void onCancleClick(ResOrderListEntity.DataBean entity) {
                mPresenter.cancleOrder(entity.getOrder_id());
            }

            @Override
            public void onPayClick(ResOrderListEntity.DataBean entity) {
                HomePayActivity.start(getContext(), String.valueOf(entity.getProduct_id()), String.valueOf(entity.getOrder_id()), String.valueOf(entity.getOrder_id()), String.valueOf(entity.getTotal_price()), "");
                //getActivity().finish();
            }
        });

        mRefreshLayout.setOnRefreshListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mRefreshLayout.setEnableLoadMore(true);
                ship = 0;
                mPresenter.getOrderListData(typeid,ship,take);
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                mPresenter.getOrderListData(typeid,ship,take);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {

            }
        });
    }

    @Override
    protected OrderListContract.Presenter createPresenter() {
        return new OrderListPresenter(this);
    }

    private boolean isFirstIn = true;

    private Handler handler = new Handler();

    //懒加载数据
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isFirstIn) {
            try {
                //todo:请求数据
                isFirstIn = false;
                Log.d("dwqdwqdwqdq","setUserVisibleHint");
                loadData();
            } catch (Exception e) {

            }
        }
    }

    private String typid;

    private int ship = 0;

    private int take = 10;

    private void loadData(){
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(getArguments() != null){
                    typid = getArguments().getString(ORDERTYPEID,"-1");
                }
                mRefreshLayout.autoRefresh();
                Log.d("dwqdqwddwq","type:"+typid);

            }
        },500);
    }

    @Override
    public void setPresenter(OrderListContract.Presenter presenter) {

    }

    private Dialog mDialog;

    @Override
    public void showLoadingDialog() {
        mDialog.show();
    }

    @Override
    public void dismissLoadingDialog() {
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
        LoadingDialog.closeDialog(mDialog);
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showToken() {
        TokenDialog.getInsence(getContext()).show();
    }

    @Override
    public void setOrderListData(ResOrderListEntity mData) {
        if(mRefreshLayout.isLoading()){
            mAdp.addData(mData.getData());
        }else if(mRefreshLayout.isRefreshing()){
            mAdp.setData(mData.getData());
        }else{
            mAdp.setData(mData.getData());
        }
        ship = mAdp.getItemCount();
        if(Integer.valueOf(mData.getCount())>mAdp.getItemCount()){
            mRefreshLayout.setEnableLoadMore(true);
        }else{
            mRefreshLayout.setEnableLoadMore(false);
        }
    }

    @Override
    public void setCancleResults(String msg) {
        showToast(msg);
        mRefreshLayout.autoRefresh();
    }
}
